package com.bit.inmap;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {
	EditText id, pwd;
	MySQLHandler handler;
	String r_id,r_pwd;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		handler = MySQLHandler.open(getApplicationContext());
		
		id = (EditText) this.findViewById(R.id.login_id);
		pwd = (EditText) this.findViewById(R.id.login_pwd);
		Button addMemberBtn = (Button) this.findViewById(R.id.add_member_btn);
		addMemberBtn.setOnClickListener(this);
		Button loginBtn = (Button) this.findViewById(R.id.login_btn);
		loginBtn.setOnClickListener(this);
		Button cancelBtn = (Button) this.findViewById(R.id.cancel_btn);
		cancelBtn.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.login, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.add_member_btn:
			Intent intent = new Intent(this,
					AddMemberActivity.class);
			startActivity(intent);
			break;
			
		case R.id.login_btn:
			
			String e_id = id.getText().toString();
			String e_pwd = pwd.getText().toString();
						
			Cursor c = handler.selectAll();
			if(c !=null){
				c.moveToFirst();
			}

			while(c.moveToNext()){
				String chId = c.getString(c.getColumnIndex("id"));
				String chPwd = c.getString(c.getColumnIndex("pwd"));
				if (e_id.equals(chId) && e_pwd.equals(chPwd)){
					r_id = chId;
					r_pwd = chPwd;
				}
				
			}
			handler.close();
			
			if (e_id.equals(r_id) && e_pwd.equals(r_pwd)) {
				MainActivity.login_check = true;
				Toast toast = Toast.makeText(this, "로그인 성공", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				intent = new Intent(this,MainActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);	// 메인이동후 이전 액티비티 스택 비우기(스택에 쌓인 액티비티 삭제)
				startActivity(intent);
				
			} else if (e_id.trim().isEmpty()){						
				Toast.makeText(this, "아이디를 입력해주세요", Toast.LENGTH_SHORT).show();					
			} else if(pwd.getText().toString().trim().isEmpty()){
				Toast.makeText(this, "비밀번호를 입력해주세요", Toast.LENGTH_SHORT).show();
			}else if((e_id.equals(r_id)) && !(e_pwd.equals(r_pwd)) ){
				Toast.makeText(this, "회원 정보가 없습니다", Toast.LENGTH_SHORT).show();					
			}else if(!(e_id.equals(r_id)) && (e_pwd.equals(r_pwd)) ){
				Toast.makeText(this, "패스워드가 올바르지 않습니다", Toast.LENGTH_SHORT).show();
			}else {
				Toast.makeText(this, "로그인 실패", Toast.LENGTH_SHORT).show();
			}
			break;
		
		case R.id.cancel_btn:
			finish();
			break;
		}
	}

}
